package finassist.DAO;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.classic.Session;
import finassist.model.Entiti;
import finassist.model.Ledger;
import finassist.model.LedgerSubType;
import finassist.model.LedgerType;
import finassist.model.Test;
import finassist.util.HibernateUtil;

/**
 * AJ Projects
 * LoginUserManagerDAO.java
 * Purpose: Login User Database.
 *
 * @author Karthick Natarajan
 * @version 1.0 9/10/13
 */

public class EntityProcessManagerDAO extends HibernateUtil {

	/********** Add new Entity *************/
	public void add(Entiti entiti) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(entiti);
		session.getTransaction().commit();
	}
	
	public void addTest(Test test) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(test);
		session.getTransaction().commit();
	}
	
public List<Ledger> getLedgerDetails() {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Ledger> ledgers = null;
		
		try {
			
			ledgers = (List<Ledger>)session.createSQLQuery("select * from public.fnas_ledger_master").addEntity(Ledger.class).list();
					
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return ledgers;
	}

public List<LedgerType> getLedgerTypeDetails() {
	
	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	session.beginTransaction();
	List<LedgerType> ledgerTypes = null;
	
	try {
		
		ledgerTypes = (List<LedgerType>)session.createSQLQuery("select * from public.fnas_ledger_type").addEntity(LedgerType.class).list();
				
	} catch (HibernateException e) {
		e.printStackTrace();
		session.getTransaction().rollback();
	}
	session.getTransaction().commit();
	return ledgerTypes;
}

public List<LedgerSubType> getLedgerSubTypeDetails() {
	
	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	session.beginTransaction();
	List<LedgerSubType> ledgerSubTypes = null;
	
	try {
		
		ledgerSubTypes = (List<LedgerSubType>)session.createSQLQuery("select * from public.fnas_ledger_sub_type").addEntity(LedgerSubType.class).list();
				
	} catch (HibernateException e) {
		e.printStackTrace();
		session.getTransaction().rollback();
	}
	session.getTransaction().commit();
	return ledgerSubTypes;
}
	
}
